package com.geralt.im.jms;


import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

public class MessageQueueConsumer {
	private Connection conn;
	private ConnectionFactory cf;
	private Session session;
	private Destination queue;
	private MessageProducer producer;
	private String url;
	private String queue_name;
	private MessageListener listener;
	MessageConsumer consumer;
	public MessageQueueConsumer(){}
	public MessageQueueConsumer(String url,String queue_name){
		this.url = url;
		this.queue_name = queue_name;
		this.createConnet();
	}
	public void createConnet(){
		cf = new ActiveMQConnectionFactory(url);
		try {
			conn = cf.createConnection();
			session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			queue = new ActiveMQQueue(queue_name);
			consumer = session.createConsumer(queue);
			conn.start();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	public void sendReply(){
		
	}
	public void stop(){
		try {
			conn.stop();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	public void start(){
		try {
			conn.start();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	public void beginListen(){
		try {
			consumer.setMessageListener(listener);
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	public void close_all(){
		try {
			session.close();
			producer.close();
			conn.close();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}
	/**
	 * @param url the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}
	/**
	 * @return the queue_name
	 */
	public String getQueue_name() {
		return queue_name;
	}
	/**
	 * @param queue_name the queue_name to set
	 */
	public void setQueue_name(String queue_name) {
		this.queue_name = queue_name;
	}
	/**
	 * @return the listener
	 */
	public MessageListener getListener() {
		return listener;
	}
	/**
	 * @param listener the listener to set
	 */
	public void setListener(MessageListener listener) {
		this.listener = listener;
	}
	/**
	 * @return the session
	 */
	public Session getSession() {
		return session;
	}
	/**
	 * @param session the session to set
	 */
	public void setSession(Session session) {
		this.session = session;
	}
	
	
}
